package com.yitao.cms.service.operatorLog;

import com.querydsl.core.types.Order;
import com.querydsl.core.types.OrderSpecifier;
import com.skipper.base.bean.CommonStatus;
import com.skipper.base.bean.PagedInput;
import com.skipper.base.bean.PagedOutput;
import com.skipper.base.data.querydsl.PredicateBuilder;
import com.skipper.base.service.GenericEntityService;
import com.yitao.client.dto.svclog.YitaoSvcLogDto;
import com.yitao.client.model.svclog.QYitaoSvcLogModel;
import com.yitao.client.model.svclog.YitaoSvcLogModel;
import com.yitao.cms.utils.BaseUtils;
import com.yitao.cms.utils.DateUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.persistence.EntityManager;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * pc备案官网操作日志
 *
 * @Date: 2019/10/10 10:57
 * @Author: Kien
 */
@Service
public class YitaoSvcLogService extends GenericEntityService<YitaoSvcLogModel, YitaoSvcLogDto, QYitaoSvcLogModel> {

    public YitaoSvcLogService(EntityManager entityManager) {
        super(entityManager);
    }

    /**
     * 查询访问日志
     * @param nickName 用户昵称
     * @param staDate 开始时间
     * @param endDate 结束时间
     * @param pageNo 当前页码
     * @param pageSize 每页显示条数
     * @return
     * @Date: 2019/10/10 10:57
     * @Author: Kien
     */
    public PagedOutput<YitaoSvcLogDto> findSystemSvcLog(String nickName, Date staDate, Date endDate, int pageNo, int pageSize) {
        PagedInput pagedInput = new PagedInput(pageSize, pageNo);
        try {
            PagedOutput<YitaoSvcLogModel> pagedOutput = repository.pagingList(PredicateBuilder.conjunction()
                    .ifNotNullThenAnd(nickName, () -> Q.nickName.like("%" + nickName + "%"))
                    .ifTrueThenAnd(staDate != null && !"".equals(staDate) && endDate != null && !"".equals(endDate), ()
                            -> Q.createTime.between(staDate, endDate))
                    .get(), pagedInput, new OrderSpecifier<>(Order.DESC, Q.createTime));
            List<YitaoSvcLogDto> list = new ArrayList<>();
            if(pagedOutput != null && pagedOutput.getData() != null && pagedOutput.getData().size() != 0) {
                list = pagedOutput.getData().stream().map(it -> {
                    YitaoSvcLogDto yitaoSvcLogDto = new YitaoSvcLogDto();
                    BeanUtils.copyProperties(it, yitaoSvcLogDto);
                    yitaoSvcLogDto.setCreateTimeStr(DateUtils.getDateSecondFormat(it.getCreateTime()));
                    if("-1".equals(it.getCreateBy())) {
                        yitaoSvcLogDto.setNickName("游客");
                        yitaoSvcLogDto.setPhone("-");
                    }
                    return yitaoSvcLogDto;
                }).collect(Collectors.toList());
            }
            return new PagedOutput(pagedInput, pagedOutput.getTotalElements(), list);
        } catch (Exception e) {
            return new PagedOutput(CommonStatus.SERVER_ERROR, pagedInput, 0, new ArrayList());
        }
    }
}
